/**
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

/*!
 * \file matmul_shape_info.h
 * \brief matmul shape info manager
 */

#ifndef IMPL_MATMUL_MODULES_MATMUL_SHAPE_INFO_H
#define IMPL_MATMUL_MODULES_MATMUL_SHAPE_INFO_H

#include "../matmul_module.h"

namespace matmul {
template <typename IMPL, const auto &MM_CFG, class INPUT_TYPE, typename = void>
class MatmulShapeInfo {
    using SrcT = typename INPUT_TYPE::T;
    using TransT = typename INPUT_TYPE::TRANS_T;
public:
    template <bool IS_TRANS = false, bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetOrgHeight()
    {
        if constexpr (IS_INTRA_BLOCK) {
            if constexpr (!IS_TRANS) {
                if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                    return Align<int32_t>(MATMUL_CONST_INTRA_BLOCK.singleCoreM, BLOCK_CUBE);
                } else {
                    return MATMUL_CONST_INTRA_BLOCK.M;
                }
            }
            if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                return Align<int32_t>(MATMUL_CONST_INTRA_BLOCK.singleCoreK, BLOCK_CUBE);
            } else {
                return MATMUL_CONST_INTRA_BLOCK.Ka;
            }
        } else {
            if constexpr (!IS_TRANS) {
                if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                    return MATMUL_CONST_PARAM_VAR.singleCoreM_;
                } else {
                    return MATMUL_CAST_TO_IMPL()->M_;
                }
            }
            if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                return MATMUL_CONST_PARAM_VAR.singleCoreK_;
            } else {
                return MATMUL_CAST_TO_IMPL()->Ka_;
            }
        }
    }

    template <bool IS_TRANS = false, bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetOrgWidth()
    {
        if constexpr (IS_INTRA_BLOCK) {
            constexpr int32_t c0Size = AuxGetC0Size<TransT>();
            if constexpr (!IS_TRANS) {
                if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                    return Align<int32_t>(MATMUL_CONST_INTRA_BLOCK.singleCoreK, c0Size);
                } else {
                    return MATMUL_CONST_INTRA_BLOCK.Ka;
                }
            }
            if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                return Align<int32_t>(MATMUL_CONST_INTRA_BLOCK.singleCoreM, c0Size);
            } else {
                return MATMUL_CONST_INTRA_BLOCK.M;
            }
        } else {
            if constexpr (!IS_TRANS) {
                if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                    return MATMUL_CONST_PARAM_VAR.singleCoreK_;
                } else {
                    return MATMUL_CAST_TO_IMPL()->Ka_;
                }
            }
            if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                return MATMUL_CONST_PARAM_VAR.singleCoreM_;
            } else {
                return MATMUL_CAST_TO_IMPL()->M_;
            }
        }
    }

    template <bool IS_TRANS = false, bool ONLY_RUNTIME = false, bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetSingleHeight() const
    {
        if constexpr (!ONLY_RUNTIME && IsBasic(MM_CFG)) {
            if constexpr (IS_TRANS) {
                return ToMatmulConfig(MM_CFG).singleCoreK;
            } else {
                return ToMatmulConfig(MM_CFG).singleCoreM;
            }
        } else {
            if constexpr (IS_INTRA_BLOCK) {
                if constexpr (IS_TRANS) {
                    return MATMUL_CONST_INTRA_BLOCK.singleCoreK;
                } else {
                    return MATMUL_CONST_INTRA_BLOCK.singleCoreM;
                }
            } else {
                if constexpr (IS_TRANS) {
                    return MATMUL_CONST_PARAM_VAR.singleCoreK_;
                } else {
                    return MATMUL_CONST_PARAM_VAR.singleCoreM_;
                }
            }
        }
    }

    template <bool IS_TRANS = false, bool ONLY_RUNTIME = false, bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetSingleWidth() const
    {
        if constexpr (!ONLY_RUNTIME && IsBasic(MM_CFG)) {
            if constexpr (IS_TRANS) {
                return ToMatmulConfig(MM_CFG).singleCoreM;
            } else {
                return ToMatmulConfig(MM_CFG).singleCoreK;
            }
        } else {
            if constexpr (IS_INTRA_BLOCK) {
                if constexpr (IS_TRANS) {
                    return MATMUL_CONST_INTRA_BLOCK.singleCoreM;
                } else {
                    return MATMUL_CONST_INTRA_BLOCK.singleCoreK;
                }
            } else {
                if constexpr (IS_TRANS) {
                    return MATMUL_CONST_PARAM_VAR.singleCoreM_;
                } else {
                    return MATMUL_CONST_PARAM_VAR.singleCoreK_;
                }
            }
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetBaseHeight() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetBaseK();
        } else {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetBaseM();
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetBaseWidth() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetBaseM();
        } else {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetBaseK();
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetTotalRow() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.kIter_;
        } else {
            return MATMUL_CONST_PARAM_VAR.mIter_;
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetTotalCol() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.mIter_;
        } else {
            return MATMUL_CONST_PARAM_VAR.kIter_;
        }
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline bool IsTranspose() const
    {
        if constexpr (IS_INTRA_BLOCK) {
            return MATMUL_CONST_INTRA_BLOCK.isTransposeA;
        } else {
            return MATMUL_CONST_PARAM_VAR.isTransposeA_;
        }
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline bool IsKRowDirec() const
    {
        return IsTranspose<IS_INTRA_BLOCK>();
    }
};

template <typename IMPL, class INPUT_TYPE, const auto &MM_CFG>
class MatmulShapeInfo<IMPL, MM_CFG, INPUT_TYPE, enable_if_t<INPUT_TYPE::TAG == InputTypeTag::B>> {
    using SrcT = typename INPUT_TYPE::T;
    using TransT = typename INPUT_TYPE::TRANS_T;
public:
    template <bool IS_TRANS = false, bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetOrgHeight()
    {
        if constexpr (IS_INTRA_BLOCK) {
            if constexpr (!IS_TRANS) {
                if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                    return Align<int32_t>(MATMUL_CONST_INTRA_BLOCK.singleCoreK, BLOCK_CUBE);
                } else {
                    return MATMUL_CONST_INTRA_BLOCK.Kb;
                }
            }
            if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                return Align<int32_t>(MATMUL_CONST_INTRA_BLOCK.singleCoreN, BLOCK_CUBE);
            } else {
                return MATMUL_CONST_INTRA_BLOCK.N;
            }
        } else {
            if constexpr (!IS_TRANS) {
                if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                    return MATMUL_CONST_PARAM_VAR.singleCoreK_;
                } else {
                    return MATMUL_CAST_TO_IMPL()->Kb_;
                }
            }
            if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                return MATMUL_CONST_PARAM_VAR.singleCoreN_;
            } else {
                return MATMUL_CAST_TO_IMPL()->N_;
            }
        }
    }

    template <bool IS_TRANS = false, bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetOrgWidth()
    {
        if constexpr (IS_INTRA_BLOCK) {
            constexpr int32_t c0Size = AuxGetC0Size<TransT>();
            if constexpr (!IS_TRANS) {
                if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                    return Align<int32_t>(MATMUL_CONST_INTRA_BLOCK.singleCoreN, c0Size);
                } else {
                    return MATMUL_CONST_INTRA_BLOCK.N;
                }
            }
            if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                return Align<int32_t>(MATMUL_CONST_INTRA_BLOCK.singleCoreK, c0Size);
            } else {
                return MATMUL_CONST_INTRA_BLOCK.Kb;
            }
        } else {
            if constexpr (!IS_TRANS) {
                if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                    return MATMUL_CONST_PARAM_VAR.singleCoreN_;
                } else {
                    return MATMUL_CAST_TO_IMPL()->N_;
                }
            }
            if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
                return MATMUL_CONST_PARAM_VAR.singleCoreK_;
            } else {
                return MATMUL_CAST_TO_IMPL()->Kb_;
            }
        }
    }

    template <bool IS_TRANS = false, bool ONLY_RUNTIME = false, bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetSingleHeight() const
    {
        if constexpr (!ONLY_RUNTIME && IsBasic(MM_CFG)) {
            if constexpr (IS_TRANS) {
                return ToMatmulConfig(MM_CFG).singleCoreN;
            } else {
                return ToMatmulConfig(MM_CFG).singleCoreK;
            }
        } else {
            if constexpr (IS_INTRA_BLOCK) {
                if constexpr (IS_TRANS) {
                    return MATMUL_CONST_INTRA_BLOCK.singleCoreN;
                } else {
                    return MATMUL_CONST_INTRA_BLOCK.singleCoreK;
                }
            } else {
                if constexpr (IS_TRANS) {
                    return MATMUL_CONST_PARAM_VAR.singleCoreN_;
                } else {
                    return MATMUL_CONST_PARAM_VAR.singleCoreK_;
                }
            }
        }
    }

    template <bool IS_TRANS = false, bool ONLY_RUNTIME = false, bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetSingleWidth() const
    {
        if constexpr (!ONLY_RUNTIME && IsBasic(MM_CFG)) {
            if constexpr (IS_TRANS) {
                return ToMatmulConfig(MM_CFG).singleCoreK;
            } else {
                return ToMatmulConfig(MM_CFG).singleCoreN;
            }
        } else {
            if constexpr (IS_INTRA_BLOCK) {
                if constexpr (IS_TRANS) {
                    return MATMUL_CONST_INTRA_BLOCK.singleCoreK;
                } else {
                    return MATMUL_CONST_INTRA_BLOCK.singleCoreN;
                }
            } else {
                if constexpr (IS_TRANS) {
                    return MATMUL_CONST_PARAM_VAR.singleCoreK_;
                } else {
                    return MATMUL_CONST_PARAM_VAR.singleCoreN_;
                }
            }
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetBaseHeight() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetBaseN();
        } else {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetBaseK();
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetBaseWidth() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetBaseK();
        } else {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetBaseN();
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetTotalRow() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.nIter_;
        } else {
            return MATMUL_CONST_PARAM_VAR.kIter_;
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetTotalCol() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.kIter_;
        } else {
            return MATMUL_CONST_PARAM_VAR.nIter_;
        }
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline bool IsTranspose() const
    {
        if constexpr (IS_INTRA_BLOCK) {
            return MATMUL_CONST_INTRA_BLOCK.isTransposeB;
        } else {
            return MATMUL_CONST_PARAM_VAR.isTransposeB_;
        }
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline bool IsKRowDirec() const
    {
        return !IsTranspose<IS_INTRA_BLOCK>();
    }
};

template <typename IMPL, class INPUT_TYPE, const auto &MM_CFG>
class MatmulShapeInfo<IMPL, MM_CFG, INPUT_TYPE, enable_if_t<INPUT_TYPE::TAG == InputTypeTag::C>> {
    using SrcT = typename INPUT_TYPE::T;
    using TransT = typename INPUT_TYPE::TRANS_T;
public:
    __aicore__ inline int32_t GetBaseHeight()
    {
        return MATMUL_PARAM_VAR.tiling_.GetBaseM();
    }

    __aicore__ inline int32_t GetBaseWidth()
    {
        return MATMUL_PARAM_VAR.tiling_.GetBaseN();
    }

    __aicore__ inline int32_t GetSingleHeight()
    {
        return MATMUL_PARAM_VAR.singleCoreM_;
    }

    __aicore__ inline int32_t GetSingleWidth()
    {
        return MATMUL_PARAM_VAR.singleCoreN_;
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetBaseUseHeight()
    {
        if constexpr (ToMatmulConfig(MM_CFG).intraBlockPartSum) {
            if constexpr (IS_INTRA_BLOCK) {
                return MATMUL_CAST_TO_IMPL()->intraBlockMatmul.baseUseM;
            }
        }
        return MATMUL_PARAM_VAR.baseUseM_;
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetBaseUseWidth()
    {
        if constexpr (ToMatmulConfig(MM_CFG).intraBlockPartSum) {
            if constexpr (IS_INTRA_BLOCK) {
                return MATMUL_CAST_TO_IMPL()->intraBlockMatmul.baseUseN;
            }
        }
        return MATMUL_PARAM_VAR.baseUseN_;
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetBlockNumAlongHeight()
    {
        if constexpr (ToMatmulConfig(MM_CFG).intraBlockPartSum) {
            if constexpr (IS_INTRA_BLOCK) {
                return MATMUL_CAST_TO_IMPL()->intraBlockMatmul.blockUseM;
            }
        }
        return MATMUL_PARAM_VAR.blockUseM_;
    }

    template <bool IS_INTRA_BLOCK = false>
    __aicore__ inline int32_t GetBlockNumAlongWidth()
    {
        if constexpr (ToMatmulConfig(MM_CFG).intraBlockPartSum) {
            if constexpr (IS_INTRA_BLOCK) {
                return MATMUL_CAST_TO_IMPL()->intraBlockMatmul.blockUseN;
            }
        }
        return MATMUL_PARAM_VAR.blockUseN_;
    }
};
}      // namespace matmul
#endif // IMPL_MATMUL_MODULES_MATMUL_SHAPE_INFO_H
